#include "XmlWriter.h"
#include <sstream>
#include <iostream>

/** \brief Writes person data in XML format
*   \param Document to write to, person to write
*   \return void
*
* Method for writing person data to TiXML Document
*/
void XmlWriter::writePerson(TiXmlDocument& doc, Person data){

    stringstream ss;

    /*Create person node*/
	TiXmlElement *person = new TiXmlElement("Person");
	person->SetAttribute("id", data.getId());
	doc.LinkEndChild(person);

    /*Add distance node*/
	ss << data.getDistance();
    string distString = ss.str();
    TiXmlElement *addDistance = addElement("Distance", distString);

    /*Add pose node*/
    ss.str("");
	ss << data.getPose();
    string poseString = ss.str();
    TiXmlElement *addPose = addElement("Pose", poseString);

     /*Add gathered node*/
    ss.str("");
	ss << data.getGathered();
    string gatherString = ss.str();
    TiXmlElement *addGather = addElement("Gathered", gatherString);

    /*Add position node*/
    ss.str("");
	ss << data.getPosition();
    string positionString = ss.str();
    TiXmlElement *addPosition = addElement("Position", positionString);

    /*Add speed node*/
    ss.str("");
	ss << data.getSpeed();
    string speedString = ss.str();
    TiXmlElement *addSpeed = addElement("Speed", speedString);

    /*Link nodes*/
	person->LinkEndChild(addDistance);
	person->LinkEndChild(addPose);
	person->LinkEndChild(addGather);
	person->LinkEndChild(addPosition);
	person->LinkEndChild(addSpeed);


}
/** \brief Create child elements
*   \param Name and data of element
*   \return Pointer to TiXmlElement
*
* Helper function for adding noded
*
*/
TiXmlElement* XmlWriter::addElement(const char* name, string text){
    const char *textPtr = text.c_str();
	TiXmlElement *element = new TiXmlElement(name);
	TiXmlText *pText = new TiXmlText(textPtr);
	element->LinkEndChild(pText);

	return element;

}



